package com.weiaibenpao.demo.chislim.sportoutdoor.presentation.module;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;
import com.umeng.socialize.PlatformConfig;
import com.weiaibenpao.demo.chislim.CrashHandler;
import com.weiaibenpao.demo.chislim.hurui.huanxin.DemoHelper;
import com.weiaibenpao.demo.chislim.service.Map_Service;
import com.weiaibenpao.demo.chislim.sportoutdoor.presentation.common.BaseApplication;

import cn.jpush.android.api.JPushInterface;

public class PedometerApplication extends BaseApplication {

    private static final String TAG = "JPush";
    public PedometerApplication() {
        super();
    }
    public static PedometerApplication app;

    @Override
    public void onCreate() {
        super.onCreate();
                /*极光推送*/
        Log.d(TAG, "[MyBaseApplication] onCreate");
        Log.i(TAG, "[MyBaseApplication] onCreate");

        JPushInterface.setDebugMode(true); 	// 设置开启日志,发布时请关闭日志
        JPushInterface.init(getApplicationContext());     		// 初始化 JPush

        ApplicationModule.initSingleton().onCreateMainProcess();
        app=this;
        // 记得使用这个fresco要在Application中初始化啊！
        //Fresco.initialize(this);
        // the following line is important Fresco.initialize(getApplicationContext());
        Fresco.initialize(getApplicationContext());

        PlatformConfig.setWeixin("wx221705f86c71d1bf", "225c77b86158d2acf80eaef2c79b433f");
        //微信 appid appsecret

        PlatformConfig.setSinaWeibo("1959713611","e62e0d810039469a8852ba366afdb0bc");
        //新浪微博 appkey appsecret

        PlatformConfig.setQQZone("1105642894", "3cvNf8EoHe17VQ93");
        // QQ和Qzone appid appkey


        // 应用程序入口处调用,避免手机内存过小,杀死后台进程后通过历史intent进入Activity造成SpeechUtility对象为null
        // 注意：此接口在非主进程调用会返回null对象，如需在非主进程使用语音功能，请增加参数：SpeechConstant.FORCE_LOGIN+"=true"
        // 参数间使用“,”分隔。
        // 设置你申请的应用appid

        // 注意： appid 必须和下载的SDK保持一致，否则会出现10407错误

        StringBuffer param = new StringBuffer();
        param.append("appid=588020d9"  );
        param.append(",");
        // 设置使用v5+
        param.append(SpeechConstant.ENGINE_MODE+"="+SpeechConstant.MODE_MSC);
        SpeechUtility.createUtility(getApplicationContext(), param.toString());

        //环形初始化
        DemoHelper.getInstance().init(this);

        //唤醒跑步服务
        //startAlarm();
//        CrashHandler.getInstance().init(this);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }

    public void startAlarm(){
        /**
         * 首先获得系统服务
         */
        AlarmManager am = (AlarmManager)
                getSystemService(Context.ALARM_SERVICE);

        /** 设置闹钟的意图，我这里是去调用一个服务，该服务功能就是获取位置并且上传*/
        Intent intent = new Intent(this, Map_Service.class);
        PendingIntent pendSender = PendingIntent.getService(this, 0, intent, 0);
        am.cancel(pendSender);

        /**AlarmManager.RTC_WAKEUP 这个参数表示系统会唤醒进程；我设置的间隔时间是10分钟 */
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60*1000, pendSender);
    }
}
